﻿using System.Collections;

namespace Dto
{
    public class PagingDto : IEnumerable
    {
        private readonly IList _rows = new ArrayList();
        private int _pageRange = 6;

        public PagingDto(int totalrows, int pageno, int pagesize, IList list)
        {
            TotalRows = totalrows;
            PageSize = pagesize;
            TotalPages = (totalrows - 1)/pagesize + 1; //取得最大页数
            FirstPageNo = 1;
            LastPageNo = TotalPages;
            CurrentPageNo = ValidPageNo(pageno);
            _pageRange = 10;
            _rows = list;
            CalculateShowPageNumber();
        }

        //private readonly IList<T> _rows = new List<T>();
        public IList Rows
        {
            get { return _rows; }
        }

        public int TotalRows { get; private set; }

        public int TotalPages { get; private set; }

        public int PageSize { get; private set; }

        public int FirstPageNo { get; private set; }

        public int LastPageNo { get; private set; }

        public int CurrentPageNo { get; private set; }

        public int PageMinNumber { get; private set; }

        public int PageMaxNumber { get; private set; }

        /// <summary>
        /// 原始路径
        /// </summary>
        public string PathValue { get; set; }

        public int PreviousPageNo
        {
            get { return (CurrentPageNo - 1) < FirstPageNo ? FirstPageNo : (CurrentPageNo - 1); }
        }

        public int NextNo
        {
            get { return (CurrentPageNo + 1) > LastPageNo ? LastPageNo : (CurrentPageNo + 1); }
        }

        public int PageRange
        {
            get { return _pageRange; }
            set { _pageRange = value <= 0 ? 6 : value; }
        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return _rows != null ? _rows.GetEnumerator() : null;
        }

        #endregion

        private int ValidPageNo(int pageno)
        {
            return (pageno < LastPageNo
                        ? (pageno > FirstPageNo
                               ? pageno
                               : FirstPageNo)
                        : LastPageNo);
        }

        private void CalculateShowPageNumber()
        {
            if (TotalPages <= _pageRange)
            {
                PageMinNumber = 1;
                PageMaxNumber = TotalPages;
            }
            else
            {
                if ((TotalPages/_pageRange) == ((CurrentPageNo - 1)/_pageRange))
                {
                    PageMinNumber = (CurrentPageNo/_pageRange)*_pageRange + 1;
                    PageMaxNumber = TotalPages;
                }
                else
                {
                    PageMinNumber = ((CurrentPageNo - 1)/_pageRange)*_pageRange + 1;
                    PageMaxNumber = ((CurrentPageNo - 1)/_pageRange + 1)*_pageRange;
                }
            }
        }
    }
}